package com.atguigu.guliguli.product.controller;

import com.atguigu.guliguli.common.utils.PageUtils;
import com.atguigu.guliguli.common.utils.R;
import com.atguigu.guliguli.product.entity.ProductAttrValueEntity;
import com.atguigu.guliguli.product.service.AttrService;
import com.atguigu.guliguli.product.service.ProductAttrValueService;
import com.atguigu.guliguli.product.vo.AttrRespVo;
import com.atguigu.guliguli.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 叶子
 * @date: 2021/11/28 18:40
 **/
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data", entities);
    }

    /**
     * //product/attr/sale/list/0？
     * //product/attr/base/list/{catalogId}
     * @param params
     * @param catalogId
     * @param attrType
     * @return
     */
    @GetMapping("/{attrType}/list/{catalogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("attrType") Long catalogId,
                          @PathVariable("catalogId") String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params,catalogId,attrType);
        return R.ok().put("page",page);
    }
    @RequestMapping("/list")
    public R list(@RequestParam Map<String,Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page",page);
    }
    @RequestMapping("/info/{attrId}")
    public R info (@PathVariable("attrId") Long attrId){
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr",respVo);
    }
    @PostMapping("/save")
    public R save(@RequestBody AttrVo attr){
        attrService.saveAttr(attr);
        return R.ok();
    }
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
        attrService.updateAttr(attr);
        return R.ok();
    }
}